//
//  XCMyProjectViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/23.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCMyProjectViewController.h"
#import "XCUrlManger.h"
#import "OnlineAVplay.h"
#import "XCAFNetTool.h"
#import "XCMyProjectModel.h"
#import "UIImageView+WebCache.h"
#import "XCMyProjectCell.h"
#import "XCMyProjectWebViewController.h"
@interface XCMyProjectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation XCMyProjectViewController

- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    [_tableView release];
    [_dataSource release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self myprojectRequest];
    [self tableViewBuild];
    self.navigationItem.title = @"本地专题";
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
}
- (void)myprojectRequest {
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [XCAFNetTool GETNetWithUrl:[XCUrlManger myProjectRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            XCMyProjectModel *myProject = [[XCMyProjectModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:myProject];
            [myProject release];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (void)tableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[XCMyProjectCell class] forCellReuseIdentifier:@"XCMyProjectCell"];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCMyProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCMyProjectCell"];
    XCMyProjectModel *myProject = self.dataSource[indexPath.row];
    cell.myProjectModel = myProject;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XCMyProjectModel *myProject = self.dataSource[indexPath.row];
    XCMyProjectWebViewController *web = [[XCMyProjectWebViewController alloc] initWithActionUrl:myProject.actionUrl];
    [self.navigationController pushViewController:web animated:YES];
    
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}






@end
